package oa.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Collection;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;

import oa.dao.FormTemplateDao;
import oa.domain.page.PageBean;
import oa.domain.workflow.FormTemplate;
import oa.service.FormTemplateService;
import oa.util.HQLHelper;

@Service("formTemplateService")
public class FormTemplateServiceImpl implements FormTemplateService {

	@Resource(name="formTemplateDao")
	private FormTemplateDao formTemplateDao;
	@Override
	public Collection<FormTemplate> getAllFormtemplate() {
		// TODO Auto-generated method stub
		return this.formTemplateDao.getAllEntry();
	}

	@Override
	@Transactional(readOnly=false)
	public void saveFormtemplate(FormTemplate formTemplate) {
		// TODO Auto-generated method stub
		this.formTemplateDao.saveEntity(formTemplate);
	}

	@Override
	@Transactional(readOnly=false)
	public void updateFormTemplate(FormTemplate formTemplate) {
		// TODO Auto-generated method stub
		this.formTemplateDao.updateEntry(formTemplate);
	}

	@Override
	@Transactional(readOnly=false)
	public void deleteFormTemplate(Long ftid) {
		// TODO Auto-generated method stub
		this.formTemplateDao.deleteEntry(ftid);
	}

	@Override
	public InputStream download(Long ftid) throws Exception {
		// TODO Auto-generated method stub
		FormTemplate formTemplate = this.formTemplateDao.getEntryById(ftid);
		String url = formTemplate.getUrl();
        String fileName = formTemplate.getName();
        fileName = URLEncoder.encode(fileName, "utf-8");
        ActionContext.getContext().put("fileName", fileName);
		return new FileInputStream(new File(url));
	}

	@Override
	public FormTemplate getFormTemplateById(Long ftid) {
		// TODO Auto-generated method stub
		return this.formTemplateDao.getEntryById(ftid);
	}

	@Override
	public PageBean getPageBean(HQLHelper hh, int currentPage) {
		// TODO Auto-generated method stub
		return this.getPageBean(hh, currentPage);
	}

}
